import time
from selenium import webdriver
from selenium.webdriver.common.by import By

# 打开浏览器--百度--
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')

# 搜索框输入--python--并搜索
time.sleep(2)
kw = driver.find_element(By.ID,'kw')
kw.send_keys('python')
driver.find_element(By.ID,'su').click()

# 第一种写法
# TODO 滚动到页面底部
time.sleep(2)
js1 = 'window.scrollTo(0,document.body.scrollHeight)'
driver.execute_script(js1)

# TODO 滚动到页面顶部
time.sleep(2)
js2 = 'window.scrollTo(0,0)'
driver.execute_script(js2)

# 第二种写法
time.sleep(2)
js3 = 'window.scrollTo({left:0,top:document.body.csrollHeinght,behavior:"smooth"})'
driver.execute_script(js3)

# 滚动到顶部
time.sleep(2)
driver.execute_script(js2)

# TODO 聚焦到某个元素
time.sleep(2)
target = driver.find_element(By.XPATH,'//*[@id="8"]/div/div[1]/h3')
driver.execute_script("arguments[0].scrollIntoView();",target)

# 退出浏览器
time.sleep(2)
driver.quit()